package net.xdclass.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.request.CartItemRequest;
import net.xdclass.request.ConfirmOrderCartItems;
import net.xdclass.service.ICartService;
import net.xdclass.utils.JsonData;
import net.xdclass.vo.CartItemVO;
import net.xdclass.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "购物车")
@RequestMapping("/api/cart/v1")
@Slf4j
public class CartController {

    @Autowired
    private ICartService cartService;

    @ApiOperation("添加到购物车")
    @PostMapping("/add")
    public JsonData addToCart(@ApiParam("购物项") @RequestBody CartItemRequest cartItemRequest){

        cartService.addToCart(cartItemRequest);
        return JsonData.buildSuccess();
    }

    @ApiOperation("清空购物车")
    @DeleteMapping("/clear")
    public JsonData clearMyCart(){

        cartService.clear();
        return JsonData.buildSuccess();
    }

    @ApiOperation("查看我的购物车")
    @GetMapping("/mycart")
    public JsonData findByMyCart(){

        CartVO cartVO = cartService.getMyCart();
        return JsonData.buildSuccess(cartVO);
    }

    @ApiOperation("删除购物项")
    @DeleteMapping("/delete/{product_id}")
    public JsonData deleteITem(@ApiParam(value = "商品id", required = true) @PathVariable("product_id") long productId){

        cartService.deleteItem(productId);
        return JsonData.buildSuccess();
    }

    @ApiOperation("修改到购物车数量")
    @PostMapping("/change")
    public JsonData changeItemNum(@ApiParam("购物项") @RequestBody CartItemRequest cartItemRequest){

        cartService.changeItemNum(cartItemRequest);
        return JsonData.buildSuccess();
    }

    /**
     * 用于订单服务，确认订单，获取对应商品的最新价格
     * @param confirmOrderCartItems
     * @return
     */
    @ApiOperation("获取对应的商品信息")
    @PostMapping("/confirm_order_cart_items")
    public JsonData confirmOrderCartItems(@ApiParam("商品id列表和订单号对象") @RequestBody ConfirmOrderCartItems confirmOrderCartItems){

        List<CartItemVO> cartItemVOList = cartService.confirmOrderCartItems(confirmOrderCartItems);

        log.info("cartItemVOList={}", cartItemVOList);
        return JsonData.buildSuccess(cartItemVOList);
    }
}
